package cc.comac.mom.webservice.impl;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cc.comac.mom.bean.soap.ProblemReceiveData;
import cc.comac.mom.dao.entity.QualityQarCheckInvoiceDao;
import cc.comac.mom.dao.entity.QualityQarProblemFormDao;
import cc.comac.mom.dao.entity.QualitySapPurchaseOrderDao;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.entity.QualityQarProblemFormEntity;
import cc.comac.mom.entity.QualitySapPurchaseOrderEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.TimeUtil;
import cc.comac.mom.util.soaputil.SoapCallUtil;
import cc.comac.mom.webservice.QualityQarProblemFormExternalService;

// 问题单与外部交互的接口
@Component
@WebService(serviceName = "QarProblemForm",targetNamespace = "http://webservice.mom.comac.cc/",endpointInterface = "cc.comac.mom.webservice.QualityQarProblemFormExternalService")
public class QualityQarProblemFormExternalServiceImpl implements QualityQarProblemFormExternalService{

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private QualitySapPurchaseOrderDao qualitySapPurchaseOrderDao;
	@Autowired
	private QualityQarCheckInvoiceDao qualityQarCheckInvoiceDao;
	@Autowired
	private QualityQarProblemFormDao qualityQarProblemFormDao;
	// 调用问题单接口，传数据，开立问题单
	@Override
	public Map<String, String> insertProblemForm(QualityQarCheckInvoiceEntity qualityQarCheckInvoiceEntity) {
		QualityQarProblemFormEntity qualityQarProblemFormEntity = new QualityQarProblemFormEntity();
		qualityQarProblemFormEntity.setBatch(qualityQarCheckInvoiceEntity.getStoveNo());// 炉批号就是供应商批次号
		qualityQarProblemFormEntity.setDeviationProcess("接收检验");
		qualityQarProblemFormEntity.setInspect(baseEntityDataUtil.getCurrentUser());
		qualityQarProblemFormEntity.setInspectDate(TimeUtil.getCurrentDateString());
		qualityQarProblemFormEntity.setMaterialCode(qualityQarCheckInvoiceEntity.getMaterialNumber());
		qualityQarProblemFormEntity.setPurchaseOrder(qualityQarCheckInvoiceEntity.getPurchaseOrder());

		QueryWrapper<QualitySapPurchaseOrderEntity> queryWrapper = new QueryWrapper<QualitySapPurchaseOrderEntity>();
		queryWrapper.eq("purchase_order", qualityQarCheckInvoiceEntity.getPurchaseOrder());
		queryWrapper.eq("material", qualityQarCheckInvoiceEntity.getMaterialNumber());
		queryWrapper.eq("pai_hao", qualityQarCheckInvoiceEntity.getPartNo());
		QualitySapPurchaseOrderEntity selectOne = qualitySapPurchaseOrderDao.selectOne(queryWrapper);

		qualityQarProblemFormEntity.setMigoNo(selectOne.getVbeln());
		qualityQarProblemFormEntity.setQarNo(qualityQarCheckInvoiceEntity.getQarNo());
		qualityQarProblemFormEntity.setSapBatch(selectOne.getBatchNumber());
		qualityQarProblemFormEntity.setSerialNum(qualityQarCheckInvoiceEntity.getSerialNumber());
		qualityQarProblemFormEntity.setSupplier(selectOne.getVendorName());
		qualityQarProblemFormEntity.setVer(selectOne.getBanCi());

			// 组装数据转成json
			Map<String,String> map = new HashMap<>();
			map.put("qarNo", qualityQarProblemFormEntity.getQarNo());
			map.put("materialCode", qualityQarProblemFormEntity.getMaterialCode());
			map.put("serialNum", qualityQarProblemFormEntity.getSerialNum());
			map.put("batch", qualityQarProblemFormEntity.getBatch());
			map.put("supplier", qualityQarProblemFormEntity.getSupplier());
			map.put("purchaseOrder", qualityQarProblemFormEntity.getPurchaseOrder());
			map.put("deviationProcess", qualityQarProblemFormEntity.getDeviationProcess());
			map.put("sapBatch", qualityQarProblemFormEntity.getSapBatch());
			map.put("migoNo", qualityQarProblemFormEntity.getMigoNo());
			map.put("inspect", qualityQarProblemFormEntity.getInspect());
			map.put("inspectDate", qualityQarProblemFormEntity.getInspectDate());
			map.put("problemClassification", null);
			map.put("project", null);
			map.put("zcm01", selectOne.getPaiHao());
			map.put("creater", baseEntityDataUtil.getCurrentUser());
			map.put("createrCode", baseEntityDataUtil.getJobNumber());
			map.put("createrDepart", "");
			map.put("createrTime", TimeUtil.fomatDate(TimeUtil.getLocalDate()));
			String json = JSONObject.toJSONString(map);
			try {
				Map<String, Object> resultMap = SoapCallUtil.invoke("http://10.13.11.219/c919/web_service/services/IQualityService?wsdl", "sendQualityOrder", json);
				if (resultMap.get("status").toString().equals("1")) {
					String uapNo = resultMap.get("uapNo").toString();
					QualityQarCheckInvoiceEntity selectById = qualityQarCheckInvoiceDao
							.selectById(qualityQarCheckInvoiceEntity.getId());
					selectById.setProblemNo(uapNo);
					selectById.setProblemStatus("新建");
					baseEntityDataUtil.updateBaseEntityData(selectById);
					// 更新问题单号
					qualityQarCheckInvoiceDao.updateById(selectById);

					// 新增问题单数据
					qualityQarProblemFormEntity.setProblemNo(uapNo);
					baseEntityDataUtil.setBaseEntityData(qualityQarProblemFormEntity);
					qualityQarProblemFormDao.insert(qualityQarProblemFormEntity);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		return null;
	}
	
	// 更新问题单状态
	@Override
	public String updateProblemStatus(ProblemReceiveData problemReceiveData) {
		Map<String,String> hashMap = new HashMap<String, String>();
		hashMap.put("status", "");
		hashMap.put("msg", "");
		try {
			QueryWrapper<QualityQarCheckInvoiceEntity> queryWrapper = new QueryWrapper<QualityQarCheckInvoiceEntity>();
			queryWrapper.eq("problem_no", problemReceiveData.getUapNo());
			QualityQarCheckInvoiceEntity selectOne = qualityQarCheckInvoiceDao.selectOne(queryWrapper);
			selectOne.setProblemStatus(problemReceiveData.getStatus());
			selectOne.setProblemMessage(problemReceiveData.getUapMes());
			selectOne.setProblemModifiedUser(problemReceiveData.getInspect());
			selectOne.setProblemModifiedTime(problemReceiveData.getInspectDate());
			qualityQarCheckInvoiceDao.updateById(selectOne);
			hashMap.put("status", "1");
			hashMap.put("msg", "");
			return hashMap.toString();
		} catch (Exception e) {
			e.printStackTrace();
			hashMap.put("status", "0");
			hashMap.put("msg", e.getMessage());
			return hashMap.toString();
		}
	}
	
}
